$(function () {
    $("#searchCondition").focus();
    $("#b_deviceCode").on('click',function () {
        layer.open({
            move: false,
            type: 2,
            title: '选择设备编码',
            area: ['900px', '500px'],
            content: 'j_select_device_list.html',
            btn: ['确定','取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                layer.close(index);
            }, cancel: function (index) {
                layer.close(index);
            }
        })
    })
    //渲染选择数据表格
    var columns = selectDataList.columnsInit();
    var table = selectDataList.gridInit(columns);
    var datas = selectDataList.dataInit();
    $("#selectDeviceTable").bootstrapTable("append",datas);
    collectionFormValid()
    //保存验证
    $("#subBtn").on('click',function () {
        submitForm();
    })
})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var selectDataList = {
        columnsInit:function(){
            return  [{
                field: 'radio',
                radio: true,
                align: 'center',
                valign: 'middle'
            }, {
                field: 'od',
                title: '序号',
                width:'2%',
                align:'center'
            }, {
                field: 'deviceCode',
                title: '设备编码',
                sortable : true
            }, {
                field: 'deviceName',
                title: '设备名称',
            }, {
                field: 'address',
                title: '安装地址'
            }]
        },
        gridInit:function(columns){
            return $("#selectDeviceTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                pageNumber: 1,
                pageSize:10,
                pageList: [
                    5,10,30, 50, 100, 200
                ],
                clickToSelect: true  //单击选中列，且复选框选中
            })

        },
        dataInit:function(){
            return  [{
                od: 1,
                deviceCode:'234242424234243',
                deviceName: 'DLC0001',
                address: 'DLC0001'
            }, {
                od: 2,
                deviceCode:'q234242424234243',
                deviceName: '设备名称',
                address: '安装地址'
            },{
                od: 3,
                deviceCode:'n234242424234243',
                deviceName: '设备名称',
                address: '安装地址'
            }]
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = selectDataList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return selectDataList;});
    } else {
        !('plugin' in _global) && (_global.selectDataList = selectDataList);
    }
}());
function collectionFormValid() {
    $("#collectionForm").validate({
        rules: {
            b_deviceCode: {
                required: true,
            },
            b_dataType: {
                required: true,
            },
            b_dataCode: {
                required: true,
                minlength: 3,
                isCode:true
            },
            b_dataRange:{
                required: true,
                minlength: 2,
                isRange: true,
            },
            b_dataScale:{
                required: true,
                range:[0,5]
            },
            b_dataCycle:{
                digits:true
            },
            b_dataYield:{
                minlength: 1,
                isYield: true,
            },
            b_dataMultiple:{
                digits:true
            },
            b_rowSort:{
                isDigits: true
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errorDevice",
        success: 'valid',
        messages: {
            b_deviceCode: {
                required: " (必填)",
            },
            b_dataType: {
                required: " (必填)",
            },
            b_dataCode:{
                required: " (必填)",
                minlength: " (不能少于 3 个字母)",
            },
            b_dataRange:{
                required: " (必填)",
                minlength: "(不能少于1个数字)",
            },
            b_dataScale:{
                required: " (必填)",
                range:"输入0-5数字"
            },
            b_dataCycle:{
                digits:"输入正整数"
            },
            b_dataYield:{
                minlength: "(不能少于1个数字)",
            },
            b_dataMultiple:{
                digits:"输入正整数"
            },
            b_rowSort:{
                isDigits: "输入正整数"
            }
        },
    });
}

function submitForm() {
    if($('#collectionForm').valid()){
        return false;
    }
    return {};
}
jQuery.validator.addMethod("isCode", function(value, element) {
    return this.optional(element) || /^[A-Z]{3}[0-9]{4}$/.test(value);
},'请输入3位大写字母＋4位数字');
jQuery.validator.addMethod("isRange", function(value, element) {
    return this.optional(element) || /^(([1-9]+)|([0-9]+\.[0-9]{1,2}))$/.test(value);
},'只能包含数字和小数点，小数点保留两位');
jQuery.validator.addMethod("isYield", function(value, element) {
    return this.optional(element) || /^(([1-9]+)|([0-9]+\.[0-9]{1,3}))$/.test(value);
},'只能包含数字和小数点，小数点保留三位');